﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLToolkit.Data;
using BLToolkit.Data.Linq;
using Entities;

namespace DAL
{
    public class TaiKhoanRepository : ITaiKhoanRepository

    {
        private DbManager db = null;
        public TaiKhoanRepository()
        { db = new DbManager("QuanLyBilliards"); }
        public TaiKhoan GetByTenUser(string tenUser)
        {
            return db.GetTable<TaiKhoan>().SingleOrDefault(tk =>tk.UserName==tenUser);
        }

        public IQueryable<TaiKhoan> GetAll()
        {
            return db.GetTable<TaiKhoan>();
        }

        public bool AddNew(TaiKhoan tkhoan)
        {
            return db.Insert(tkhoan)>0;
        }

        public bool Update(TaiKhoan tkhoan)
        {
            return db.Update(tkhoan) > 0;
        }

        public bool Delete(TaiKhoan tkhoan)
        {
            return db.Delete(tkhoan) > 0;
        }


        public TaiKhoan GetByNguoiDung(string nguoiDung)
        {
            return db.GetTable<TaiKhoan>().SingleOrDefault(tk => tk.UserName == nguoiDung);
        }

        public bool KiemTraDangNhap(string ten, string pass)
        {
            TaiKhoan tk = GetByTenUser(ten);
            if (tk != null)
            {
                if (tk.Password == pass)
                    return true;
            }
            return false;
        }




        public List<TaiKhoan> TimTheoTenNguoiDN(string tennd)
        {
            return db.GetTable<TaiKhoan>().Where(n => n.TenUser == tennd).ToList();
        }

        public List<TaiKhoan> TimTheoTenTaiKhoan(string user)
        {
            return db.GetTable<TaiKhoan>().Where(n => n.UserName == user).ToList();
        }
        bool ITaiKhoanRepository.AddNew(TaiKhoan tkhoan)
        {
            return db.Insert(tkhoan) > 0;
        }
    }
}
